//
//  ESWeChatDAO.m
//  EasyStyle
//
//  Created by FK on 15/12/30.
//  Copyright © 2015年 FK. All rights reserved.
//

#import "ESWeChatDAO.h"
#import "ESWeixinNetResult.h"

@implementation ESWeChatDAO

+ (instancetype)shareInstance
{
    static ESWeChatDAO *shareInstance = nil;
    static dispatch_once_t onece;
    dispatch_once(&onece,^{
        shareInstance = [[self alloc]init];
        [shareInstance createDataBase];
    });
    return shareInstance;
}

- (id)init
{
    self = [super init];
    if (self) {
         NSString *DBPath = [ESDBBaseDAO applicationDocumentsDirectoryFile:WechatDB];
        _queue = [FMDatabaseQueue databaseQueueWithPath:DBPath];
    }
    return self;
}


/**
*  创建数据库
*/
- (void)createDataBase
{
    [_queue inDatabase:^(FMDatabase *db) {
        
        //CREATE TABLE IF NOT EXISTS t_WeChat_News (id integer PRIMARY KEY AUTOINCREMENT, newsID text NOT NULL,url text NOT NULL,title text NOT NULL,source text,firstImg text,mark text);
        
        BOOL result=[db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_WeChat_News (newsID text NOT NULL PRIMARY KEY,url text NOT NULL,title text NOT NULL,source text,firstImg text,mark text);"];
        if (result) {
            NSLog(@"微信精选 -- 创表成功");
        }else {
            NSLog(@"微信精选 -- 创表失败");
        }
    }];
}


/**
 *  插入数据
 *  @param 文章列表
 *  @return 存储是否成功
 */
- (BOOL)insertCityDBWith:(NSArray *)newsList
{
    __block BOOL result = NO;
    if (newsList != nil && newsList.count > 0) {
        [_queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
            
            NSArray *newsListCopy = [NSArray arrayWithArray:newsList];
            
            for (WxNewsObject *item in newsListCopy) {
                NSString *tmp = [db stringForQuery:@"SELECT newsID FROM t_WeChat_News WHERE newsID = ?",item.newsID];
                if (tmp == nil) {
                    @try {
                        result = [db executeUpdate:@"INSERT INTO t_WeChat_News (newsID,url,title,source,firstImg,mark) VALUES (?,?,?,?,?,?)",item.newsID,item.url,item.title,item.source,item.firstImg,item.mark];
                    }
                    @catch (NSException *exception) {
                        *rollback = YES;
                    }
                }else {
                    result = YES;
                }
            }
        }];
    }
    return  result;
}

/**
 *  查询所有数据
 *  @return 文章列表数组
 */
- (NSMutableArray *)queryAllFromDB
{
    __block NSMutableArray *arr;
    //if (index != nil) {
        [_queue inDatabase:^(FMDatabase *db) {
            FMResultSet * rs =[db executeQuery:@"SELECT * FROM t_WeChat_News"];
            arr = [NSMutableArray arrayWithCapacity:[rs columnCount]];
            while ([rs next]) {
                WxNewsObject *item = [[WxNewsObject alloc]init];
                item.newsID =[rs stringForColumn:@"newsID"];
                item.url =[rs stringForColumn:@"url"];
                item.title =[rs stringForColumn:@"title"];
                item.source =[rs stringForColumn:@"source"];
                item.firstImg =[rs stringForColumn:@"firstImg"];
                item.mark =[rs stringForColumn:@"mark"];
                [arr addObject:item];
            }
            [rs close];
        }];
  //  }
    return arr;
}

/**
 *  删除所有数据
 *  @return 删除是否成功
 */
- (BOOL)deleteAllFromDB
{
    __block BOOL result;
    [_queue inDatabase:^(FMDatabase *db) {
        result =  [db executeUpdate:@"delete from t_WeChat_News"];
    }];
    return result;
}

@end
